/*
Copyright 2022.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1beta1

import (
	v1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
	AppUpdateStrategyTypeReCreate = "ReCreate"
	AppUpdateStrategyTypeRolling  = "Rolling"
	AppUpdateStrategyTypeInPlace  = "InPlace"
)

// EDIT THIS FILE!  THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required.  Any new fields you add must have json tags for the fields to be serialized.

// AppSpec defines the desired state of App
type AppSpec struct {
	// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
	// Important: Run "make" to regenerate code after modifying this file

	Selector       map[string]string       `json:"selector"`
	Replicas       int                     `json:"replicas"`
	Image          string                  `json:"image"`
	Resources      v1.ResourceRequirements `json:"resources,omitempty"`
	Ports          []*AppSpecPort          `json:"ports,omitempty"`
	Ingresses      []AppSpecIngress        `json:"ingresses,omitempty"`
	UpdateStrategy *AppUpdateStrategy      `json:"updateStrategy,omitempty"`
}

type AppUpdateStrategy struct {
	// 百分比
	Percent *int `json:"percent,omitempty"`
	// ReCreate
	// Rolling
	// InPlace 原地部署
	Type string `json:"type,omitempty"`
}

type AppSpecPort struct {
	Name          string `json:"name"`
	Protocol      string `json:"protocol,omitempty"`
	ServicePort   int32  `json:"servicePort"`
	ContainerPort int    `json:"containerPort"`
}

type AppSpecIngress struct {
	Name        string                `json:"name"`
	ServicePort int32                 `json:"servicePort"`
	Routes      []AppSpecIngressRoute `json:"routes"`
}

type AppSpecIngressRoute struct {
	Hosts []string `json:"hosts"`
	Paths []string `json:"paths"`
}

// AppStatus defines the observed state of App
type AppStatus struct {
	// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
	// Important: Run "make" to regenerate code after modifying this file
	Replicas      int      `json:"replicas"`
	ReadyReplicas int      `json:"readyReplicas"`
	Endpoints     []string `json:"endpoints"`
}

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status

// App is the Schema for the apps API
type App struct {
	metav1.TypeMeta   `json:",inline"`
	metav1.ObjectMeta `json:"metadata,omitempty"`

	Spec   AppSpec   `json:"spec,omitempty"`
	Status AppStatus `json:"status,omitempty"`
}

//+kubebuilder:object:root=true

// AppList contains a list of App
type AppList struct {
	metav1.TypeMeta `json:",inline"`
	metav1.ListMeta `json:"metadata,omitempty"`
	Items           []App `json:"items"`
}

func init() {
	SchemeBuilder.Register(&App{}, &AppList{})
}
